package com.ruoyi.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.ruoyi.auth.form.ZhuceBody;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.user.api.RemoteCarUserService;
import com.ruoyi.user.api.domain.CarUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.auth.form.LoginBody;
import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;

import java.util.HashMap;
import java.util.Map;

/**
 * token 控制
 * 
 * @author ruoyi
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private RemoteUserService remoteUserService;

    //引入移动端用户的远程服务
    @Autowired
    private RemoteCarUserService remoteCarUserService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @PostMapping("userLogin")
    public R<?> userLogin(@RequestBody LoginBody form)
    {
        // 调用移动端用户的远程服务
        CarUser carUser = remoteCarUserService.getUserInfoByUserName(form.getUsername(), SecurityConstants.INNER);
//        System.out.println(carUser);
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        Map<String, Object> tokenMap = tokenService.createToken(userInfo);
        // 返回登录token和用户信息
        Map<String, Object> result = new HashMap<>();
        result.put("token", tokenMap); // 原token信息
        result.put("carUser", carUser); // 新增的CarUser信息
        return R.ok(result);
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
    @PostMapping("register2")
    public R<?> register2(@RequestBody ZhuceBody zhuceBody)
    {
        // 用户注册
        sysLoginService.register2(zhuceBody.getUsername(), zhuceBody.getPassword(),zhuceBody.getDeptId());
        return R.ok();
    }

    @GetMapping("sendSms")
    public R<?> sendSms(@RequestParam String phoneNumber) {
        String key = sysLoginService.sendSms(phoneNumber);
        return R.ok(key);
    }

    @GetMapping("loginBySms")
    public R<?> loginBySms(@RequestParam String phoneNumber, @RequestParam String code, @RequestParam String key) {
//        boolean b = sysLoginService.login2(code, key);
//        if (!b){
//            return R.fail("验证码错误");
//        }
        LoginUser loginUser =sysLoginService.login(phoneNumber, "123456");
        return R.ok(tokenService.createToken(loginUser));
    }
}
